package com.example.myprojectoffeixue;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.apache.http.entity.StringEntity;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by ASUS on 2017/2/14.
 */
public class Housefg extends Fragment {
    GridView gd;
    MyApplication m;
    public String id_house;
    Dialog dialog;
    adapter_house ah;
    public  int position_house;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
         View r = inflater.inflate(R.layout.house_fg, container, false);// 关联布局文件
         gd= (GridView) r.findViewById(R.id.gd_mcj);
         m= (MyApplication) getActivity().getApplication();
        gd.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                 m.hu=m.all_House.get(position);
                 Intent it=new Intent(getActivity(),HouseDeviceActivity.class);
                 startActivity(it);
            }
        });
        gd.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                id_house=m.all_House.get(position).id;
                position_house=position;
                dialog = new Dialog(getActivity(),R.style.dialog);
                dialog.setContentView(R.layout.yaokongqidialog);
                TextView tv = (TextView) dialog.findViewById(R.id.restudy);
                TextView delete = (TextView) dialog.findViewById(R.id.delete);
                TextView iv1 = (TextView) dialog.findViewById(R.id.iv1_mydialog);
                iv1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });
                tv.setVisibility(View.GONE);
                dialog.show();
                delete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

                        builder.setMessage("确认删除吗?");

                        builder.setTitle("提示");

                        builder.setPositiveButton("确认", new Dialog.OnClickListener() {

                            @Override

                            public void onClick(DialogInterface dialog, int which) {
                               deleteroom();
                            }
                        });
                        builder.setNegativeButton("取消", new Dialog.OnClickListener() {

                            @Override

                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                        builder.create().show();
                    }
                });

                return true;
            }
        });
        return r;
    }
    @Override
    public void onStart() {
        gethouse();
        super.onStart();
    }

    public void gethouse(){

        m.Nodevice_house=null;
        HouseUtil h = new HouseUtil();
        h.userId=m.userId;
        String j = JSON.toJSONString(h);
        j = AESUtil.encrypt(j);
        HttpUtils httpUtils = new HttpUtils();
        RequestParams params = new RequestParams();
        params.setHeader("Content-Type", "application/json");
        try {
            params.setBodyEntity(new StringEntity(j, "utf-8"));
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        httpUtils.send(HttpRequest.HttpMethod.POST, HttpConnection.getDeviceListByRoom, params,

                new RequestCallBack<String>() {

                    @Override
                    public void onFailure(HttpException arg0, String arg1) {
                        ToastUtils.showToast(getActivity(),"网络异常");
                    }
                    @Override
                    public void onSuccess(ResponseInfo<String> arg0) {
                        if (arg0.result != null && !("".equals(arg0.result))) {
                           arg0.result = AESUtil.decrypt(arg0.result);
                            Log.d("result",arg0.result);
                            JSONObject jb = JSONObject.parseObject(arg0.result);
                            String msg = jb.getString("msg");
                            if ("成功".equals(msg)) {
                                JSONArray ja=jb.getJSONArray("deviceList");
                               m.all_House= (ArrayList<HouseUtil>) JSONArray.parseArray(ja.toString(),HouseUtil.class);
                                for(int i=0;i<m.all_House.size();i++){
                                    if("未分配".equals(m.all_House.get(i).name)){
                                        m.Nodevice_house=m.all_House.get(i);
                                        addname();
                                    }
                                }
                                ah=new adapter_house(getActivity(),m.all_House);
                                gd.setAdapter(ah);

                            }else{
                                HttpConnection.toastLog("服务器错误", getActivity());
                            }
                        } else {
                            HttpConnection.toastLog("服务器错误", getActivity());
                        }
                    }

                });

    }
    public void deleteroom(){
        HouseUtil h = new HouseUtil();
        h.id=id_house;
        String j = JSON.toJSONString(h);
        j = AESUtil.encrypt(j);
        HttpUtils httpUtils = new HttpUtils();
        RequestParams params = new RequestParams();
        params.setHeader("Content-Type", "application/json");
        try {
            params.setBodyEntity(new StringEntity(j, "utf-8"));
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        httpUtils.send(HttpRequest.HttpMethod.POST, HttpConnection.deleteLabel, params,

                new RequestCallBack<String>() {

                    @Override
                    public void onFailure(HttpException arg0, String arg1) {
                        ToastUtils.showToast(getActivity(),"网络异常");
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> arg0) {
                        if (arg0.result != null && !("".equals(arg0.result))) {
                            arg0.result = AESUtil.decrypt(arg0.result);
                            Log.d("result",arg0.result);
                            JSONObject jb = JSONObject.parseObject(arg0.result);
                            String msg = jb.getString("msg");
                            if ("成功".equals(msg)) {
                                m.all_House.remove(position_house);
                                ah.notifyDataSetChanged();
                                gethouse();
                            }else{
                                HttpConnection.toastLog("服务器错误", getActivity());
                            }
                        } else {
                            HttpConnection.toastLog("服务器错误", getActivity());
                        }
                    }

                });











    }




















public void addname(){

    for (int i = 0; i < m.Nodevice_house.devices.size(); i++) {

            if ("0100".equals(m.Nodevice_house.devices.get(i).deviceId)) {
                // 开关
                if (m.Nodevice_house.devices.get(i).deviceName == null) {
                    m.Nodevice_house.devices.get(i).deviceName = "智能开关";
                }

            } else if ("010B".equals(m.Nodevice_house.devices.get(i).deviceId) || "010A".equals(m.Nodevice_house.devices.get(i).deviceId)) {
                // 门锁
                if (m.Nodevice_house.devices.get(i).deviceName == null) {
                    m.Nodevice_house.devices.get(i).deviceName = "智能门锁";
                }

            } else if ("010D".equals(m.Nodevice_house.devices.get(i).deviceId)) {
                // 门磁
                if (m.Nodevice_house.devices.get(i).deviceName == null) {
                    m.Nodevice_house.devices.get(i).deviceName = "智能门磁";
                }
            } else if ("010E".equals(m.Nodevice_house.devices.get(i).deviceId)) {
                // 红外
                if (m.Nodevice_house.devices.get(i).deviceName == null) {
                    m.Nodevice_house.devices.get(i).deviceName = "红外人体感应";
                }
            } else if ("010C".equals(m.Nodevice_house.devices.get(i).deviceId)) {
                // 取电开关
                if (m.Nodevice_house.devices.get(i).deviceName == null) {
                    m.Nodevice_house.devices.get(i).deviceName = "取电开关";
                }

            } else if ("0002".equals(m.Nodevice_house.devices.get(i).deviceId)) {

                // 窗帘
                if (m.Nodevice_house.devices.get(i).deviceName == null) {
                    m.Nodevice_house.devices.get(i).deviceName = "智能窗帘";
                }
            } else if ("010F".equals(m.Nodevice_house.devices.get(i).deviceId)) {
                if (m.Nodevice_house.devices.get(i).deviceName == null) {
                    m.Nodevice_house.devices.get(i).deviceName = "中央空调";
                }

            } else if ("0020".equals(m.Nodevice_house.devices.get(i).deviceId)) {
                if (m.Nodevice_house.devices.get(i).deviceName == null) {
                    m.Nodevice_house.devices.get(i).deviceName = "红外转发器";
                }

            } else if ("0009".equals(m.Nodevice_house.devices.get(i).deviceId)) {
                if (m.Nodevice_house.devices.get(i).deviceName == null) {
                    m.Nodevice_house.devices.get(i).deviceName = "智能插座";
                }
        }
    }
}














}